package com.lb.shopguide.ui.dialog;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.google.zxing.WriterException;
import com.lb.baselib.base.DialogBase;
import com.lb.shopguide.GuideApplication;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.UserBean;
import com.lb.shopguide.util.lb.BarcodeUtil;
import com.lb.shopguide.util.pic.ImageLoaderUtils;

import java.io.ByteArrayOutputStream;

/**
 * Created by kang on 2017/6/20.
 */

public class DialogShowBarcode extends DialogBase {
    private UserBean userBean;

    private TextView tvGuiderName;
    private TextView tvShopName;
    private ImageView ivBarcode;
    private ImageView ivShopLogo;
    private ImageButton ibClose;

    public static DialogShowBarcode getDialogShowBarcode(UserBean userBean) {
        DialogShowBarcode dialogShowBarcode = new DialogShowBarcode();
        Bundle bundle = new Bundle();
        bundle.putSerializable(AppConstant.USER_BEAN, userBean);
        dialogShowBarcode.setArguments(bundle);
        return dialogShowBarcode;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View mFragmentView = inflater.inflate(R.layout.dialog_show_barcode, container, false);
        initData();
        initView(mFragmentView);

//        setDialogPosition();
        bindData();
        return mFragmentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setStyle(STYLE_NO_TITLE, R.style.Theme_Base_AppCompat_Dialog_Light);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }
    }

    private void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.USER_BEAN)) {
            userBean = (UserBean) bundle.getSerializable(AppConstant.USER_BEAN);
        }
    }

    private void initView(View view) {
        ibClose = view.findViewById(R.id.ib_close);
        tvGuiderName = view.findViewById(R.id.tv_guider_name);
        tvShopName = view.findViewById(R.id.tv_shop_name);
        ivBarcode = view.findViewById(R.id.iv_barcode);
        ivShopLogo = view.findViewById(R.id.iv_shop_logo);

        ibClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogShowBarcode.this.dismiss();
            }
        });
    }

    private void bindData() {
        try {
            Bitmap bitmap = BarcodeUtil.createQRCode(userBean.getQrUrl(), (int) (210 * GuideApplication.getAppContext().getResources().getDisplayMetrics().density));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            byte[] bytes = baos.toByteArray();
            RequestOptions options = new RequestOptions()
                    .fitCenter()
                    .error(R.drawable.iv_default_avatar)
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.NONE);
            ImageLoaderUtils.getGlideManager().load(bytes).apply(options).into(ivBarcode);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        tvShopName.setText(userBean.getStoreName());
        tvGuiderName.setText(userBean.getStaffNickName());
        ImageLoaderUtils.displayCircle(getContext(), ivShopLogo, userBean.getStaffHeadPicUrl());
    }
}
